<template>
  <div class="tab flex">
    <div
      v-for="(item, index) in items"
      :key="index"
      class="tab-item cursor-pointer w-108px flex"
      :class="{ active: item.id === topCategory }"
      @click="onClick(item)"
    >
      <div class="relative">
        {{ item.name }}
        <transition name="fade">
          <div v-if="item.id == topCategory" class="absolute -bottom-4px -right-7px">
            <img :src="mallTab" alt="" />
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from 'vue'
import mallTab from '@renderer/assets/mall-tab.svg'
import { ITabItems } from './type'
import { useListStore } from '@renderer/store/modules/list'
export default defineComponent({
  name: 'Tab',
  props: {
    items: {
      type: Array as PropType<ITabItems[]>,
      default: () => []
    },
    defaultActive: {
      type: Number,
      default: () => 0
    }
  },
  emits: ['onChange'],
  setup(props, { emit }) {
    const listStore = useListStore()
    const topCategory = computed(() => {
      return listStore.topCategory
    })
    const onClick = (item) => {
      emit('onChange', item)
    }
    return {
      ...props,
      mallTab,
      onClick,
      topCategory
    }
  }
})
</script>

<style scoped>
.tab-item {
  color: #e3e3e3;
  font-size: 14px;
  font-weight: 400;
  opacity: 0.6;
  box-sizing: border-box;
}
.active {
  opacity: 1;
  font-weight: 500;
  animation-name: lineractive;
  animation-duration: 1s;
}
@keyframes lineractive {
  from {
    opacity: 0.6;
    font-weight: 400;
  }
  to {
    opacity: 1;
    font-weight: 500;
  }
}
</style>
